/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.information.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.information.domain.Article;
import me.zhengjie.modules.information.domain.req.ArticleDeleteCmd;
import me.zhengjie.modules.information.service.ArticleService;
import me.zhengjie.modules.information.domain.vo.ArticleQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author yiwei.he
* @date 2024-04-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "资讯管理")
@RequestMapping("/api/article")
@Slf4j
public class ArticleController {

    private final ArticleService articleService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/query/download")
//    @PreAuthorize("@el.check()")
    public void exportArticle(HttpServletResponse response, ArticleQueryCriteria criteria) throws IOException {
        articleService.download(articleService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询资讯（小程序）")
    @ApiOperation("查询资讯（小程序）")
    @AnonymousAccess
    public ResponseEntity<PageResult<Article>> getArticleList(ArticleQueryCriteria criteria, Page<Object> page){
        log.info("/api/article/ 查询资讯：criteria:{},page:{}",criteria,page);
        return new ResponseEntity<>(articleService.getArticleList(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/query")
    @Log("查询资讯（后管）")
    @ApiOperation("查询资讯（后管）")
    public ResponseEntity<PageResult<Article>> queryArticle(ArticleQueryCriteria criteria, Page<Object> page){
        log.info("/api/article/ 查询资讯（后管）：criteria:{},page:{}",criteria,page);
        return new ResponseEntity<>(articleService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增资讯")
    @ApiOperation("新增资讯")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> createArticle(@Validated @RequestBody Article resources){
        log.info("/api/article/ 新增资讯：{}",resources);
        articleService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改资讯")
    @ApiOperation("修改资讯")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> updateArticle(@Validated @RequestBody Article resources){
        log.info("/api/article/ 修改资讯：{}",resources);
        articleService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除资讯")
    @ApiOperation("删除资讯")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> deleteArticle(@RequestBody Set<Long> ids) {
        log.info("/api/article/ 删除资讯：{}",ids);
        articleService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/detail")
    @Log("资讯详情")
    @ApiOperation("查询资讯")
    public ResponseEntity<Article> queryArticle(@RequestBody ArticleQueryCriteria criteria){
        log.info("/api/article/detail 查询资讯详情：{}",criteria);
        return new ResponseEntity<>(articleService.getArticleDetail(criteria),HttpStatus.OK);
    }

    @PostMapping("/statusUpdate")
    @Log("发布/废除 资讯文章")
    @ApiOperation("发布/废除 资讯文章")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> publishArticle(@Validated @RequestBody Article resources) {
        log.info("/api/article/statusUpdate 发布/废除 资讯文章：{}",resources);
        if (resources.getId() != null && resources.getStatus() != null) {
            // 将咨询的状态改为发布状态,设置发布时间
            articleService.update(new UpdateWrapper<Article>()
                    .eq("id", resources.getId())
                    .set("status", resources.getStatus())
                    .set("update_time", LocalDateTime.now())
                    .set("publish_time", LocalDateTime.now())
            );
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


}
